package com.dj.mingkong.portal.biz.service.music.impl;

import com.dj.mingkong.common.constant.CommonConstants;
import com.dj.mingkong.common.constant.RedisConstants;
import com.dj.mingkong.common.enums.StatusEnum;
import com.dj.mingkong.common.infrastructure.eo.database.music.TypeEo;
import com.dj.mingkong.common.util.SmartBeanCopy;
import com.dj.mingkong.portal.biz.controller.music.vo.response.TypeVo;
import com.dj.mingkong.portal.biz.repository.database.music.TypeRepository;
import com.dj.mingkong.portal.biz.service.music.TypeService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 音乐类型服务实现类
 *
 * @author zenglonghui
 * @date: 2022/7/20 16:39
 */
@Slf4j
@Service("typeService")
public class TypeServiceImpl implements TypeService {
    @Resource
    private TypeRepository typeRepository;
    @Resource
    private RedisTemplate<String, List<TypeVo>> redisTemplate;

    @Override
    public List<TypeVo> getParentList() {
        final List<TypeEo> typeEos = typeRepository.lambdaQuery()
                .eq(TypeEo::getParentId, CommonConstants.DEFAULT_ID).orderByAsc(TypeEo::getSort).list();
        final List<TypeVo> typeVos = SmartBeanCopy.copy(typeEos, TypeVo.class);
        setDisabled(typeVos);
        return typeVos;
    }

    @Override
    public List<TypeVo> listTree() {
        final List<TypeEo> allEos = typeRepository.list();
        if (CollectionUtils.isEmpty(allEos)) {
            return Collections.emptyList();
        }
        final List<TypeVo> allVos = SmartBeanCopy.copy(allEos, TypeVo.class);
        final List<TypeVo> vos = allVos.stream().filter(vo ->
                CommonConstants.DEFAULT_ID.equals(vo.getParentId())).toList();
        setChildren(vos, allVos);
        return vos;
    }

    @Override
    public List<TypeVo> listAll() {
        final List<List<TypeVo>> range = redisTemplate.opsForList().range(RedisConstants.MUSIC_TYPE_ALL, 0, 1);
        if (CollectionUtils.isNotEmpty(range)) {
            return range.get(0);
        }
        final List<TypeEo> typeEos = typeRepository.lambdaQuery().orderByAsc(TypeEo::getSort).list();
        final List<TypeVo> typeVos = SmartBeanCopy.copy(typeEos, TypeVo.class);
        setDisabled(typeVos);
        redisTemplate.opsForList().leftPush(RedisConstants.MUSIC_TYPE_ALL, typeVos);
        return typeVos;
    }

    private void setDisabled(List<TypeVo> typeVos) {
        if (CollectionUtils.isNotEmpty(typeVos)) {
            typeVos.forEach(vo -> vo.setDisabled(StatusEnum.INVALID.equalsIntVal(vo.getStatus())));
        }
    }

    private void setChildren(List<TypeVo> vos, List<TypeVo> allVos) {
        vos.forEach(vo -> {
            vo.setDisabled(!StatusEnum.VALID.equalsIntVal(vo.getStatus()));
            final List<TypeVo> childrenList = allVos.stream().filter(item ->
                    vo.getId().equals(item.getParentId())).toList();
            if (CollectionUtils.isNotEmpty(childrenList)) {
                vo.setChildren(childrenList);
                setChildren(childrenList, allVos);
            }
        });
    }
}
